package com.alex.util.json;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * @author alex
 */
public class JsonUtils {
    public JsonUtils() {
    }

    public static StringBuilder listToJson(List<?> lists, StringBuilder sb) {
        if (sb == null) {
            sb = new StringBuilder();
        }

        sb.append("[");

        for (int i = 0; i < lists.size(); ++i) {
            Object o = lists.get(i);
            if (o instanceof Map) {
                Map<?, ?> map = (Map) o;
                mapToJson(map, sb);
            } else if (o instanceof List) {
                List<?> l = (List) o;
                listToJson(l, sb);
            } else {
                sb.append("\"" + o + "\"");
            }

            if (i != lists.size() - 1) {
                sb.append(",");
            }
        }

        sb.append("]");
        return sb;
    }

    public static StringBuilder mapToJson(Map<?, ?> map, StringBuilder sb) {
        if (sb == null) {
            sb = new StringBuilder();
        }

        sb.append("{");
        Iterator iter = map.entrySet().iterator();

        while (iter.hasNext()) {
            Entry<?, ?> entry = (Entry) iter.next();
            String key = entry.getKey() != null ? entry.getKey().toString() : "";
            sb.append("\"" + stringToJson(key) + "\":");
            Object o = entry.getValue();
            if (o instanceof List) {
                List<?> l = (List) o;
                listToJson(l, sb);
            } else if (o instanceof Map) {
                Map<?, ?> m = (Map) o;
                mapToJson(m, sb);
            } else {
                String val = entry.getValue() != null ? entry.getValue().toString() : "";
                sb.append("\"" + stringToJson(val) + "\"");
            }

            if (iter.hasNext()) {
                sb.append(",");
            }
        }

        sb.append("}");
        return sb;
    }

    private static String stringToJson(String str) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < str.length(); ++i) {
            char c = str.charAt(i);
            switch (c) {
                case '\b':
                    sb.append("\\b");
                    break;
                case '\t':
                    sb.append("\\t");
                    break;
                case '\n':
                    sb.append("\\n");
                    break;
                case '\f':
                    sb.append("\\f");
                    break;
                case '\r':
                    sb.append("\\r");
                    break;
                case '"':
                    sb.append("\\\"");
                    break;
                case '/':
                    sb.append("\\/");
                    break;
                case '\\':
                    sb.append("\\\\");
                    break;
                default:
                    sb.append(c);
            }
        }

        return sb.toString();
    }

    public static TreeMap<String, String> jsonToMap(JSONObject jsonObj) throws JSONException {
        TreeMap<String, String> params = new TreeMap();
        Iterator jsonKeys = jsonObj.keySet().iterator();

        while (jsonKeys.hasNext()) {
            String name = (String) jsonKeys.next();
            Object value = jsonObj.get(name);
            params.put(name, String.valueOf(value));
        }

        return params;
    }
}
